package com.ethannhu.todaynews.component.splash

import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams
import androidx.core.content.ContextCompat.getColor
import androidx.fragment.app.FragmentManager
import com.ethannhu.superui.process.SuperProcessUtil
import com.ethannhu.superui.util.ScreenUtil
import com.ethannhu.superui.util.SuperTextUtil
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.databinding.FragmentDialogTermServiceBinding
import com.ethannhu.todaynews.fragment.BaseViewModelDialogFragment


class TermsServiceDialogFragment : BaseViewModelDialogFragment<FragmentDialogTermServiceBinding>() {
    private lateinit var onAgreementClickListener: View.OnClickListener

    companion object {
        fun show(fragmentManager: FragmentManager, onAgreementClickListener: View.OnClickListener) {
            val dialogFragment = TermsServiceDialogFragment()

            dialogFragment.onAgreementClickListener = onAgreementClickListener
            dialogFragment.show(fragmentManager, "TermsServiceDialogFragment")
        }
    }

    override fun onResume() {
        super.onResume()
        //修改宽度，默认比AlertDialog.Builder显示对话框宽度窄，看着不好看
        //参考: https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height
        val params: ViewGroup.LayoutParams = dialog!!.window!!.attributes
        params.width = ((ScreenUtil.getScreenWith(requireContext()) * 0.9)).toInt()
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog!!.window!!.attributes = params as LayoutParams
    }

    override fun initViews() {
        super.initViews()
        // 点击外面无法关闭
        isCancelable = false

        SuperTextUtil.setLinkColor(binding.content, getColor(requireContext(), R.color.link))

    }

    override fun initListeners() {
        super.initListeners()
        //同意按钮点击
        binding.primary.setOnClickListener {
            dismiss()
            onAgreementClickListener.onClick(it) //调用其他页面的onAgreementClickListener的onclick逻辑
        }
        //不同意按钮点击
        binding.disagree.setOnClickListener {
            dismiss()
            SuperProcessUtil.killApp()
        }
    }

    override fun initData() {
        super.initData()

        //通过这个方法 转化HTML标签
        val content = Html.fromHtml(getString(R.string.term_service_privacy_content), Html.FROM_HTML_MODE_LEGACY)
        binding.content.text = content
    }


}